﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Reflection;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
	[Serializable, AttributeUsage(AttributeTargets.Method)]
	public class RecordParametersAttribute : Attribute, IReadableAttribute
	{
		public string MethodNameBefore { get; set; }
		public string MethodNameAfter { get; set; }
		public bool OnlyWhenRecordingMocks { get; set; }
		public bool AllowMockReplay { get; set; }
		public object Value { get { return MethodNameBefore; } }

		public RecordParametersAttribute(string methodNameBefore, string methodNameAfter)
		{
			MethodNameBefore = methodNameBefore;
			MethodNameAfter = methodNameAfter;
		}

		public RecordParametersAttribute(string methodNameBefore, string methodNameAfter, bool onlyWhenRecordingMocks)
			: this(methodNameBefore, methodNameAfter)
		{
			OnlyWhenRecordingMocks = onlyWhenRecordingMocks;
		}

		public RecordParametersAttribute(bool onlyWhenRecordingMocks, bool allowMockReplay)
		{
			OnlyWhenRecordingMocks = onlyWhenRecordingMocks;
			AllowMockReplay = allowMockReplay;
		}

		public MethodInfo GetMethodAfter(Type t)
		{
			if (string.IsNullOrEmpty(MethodNameAfter))
				return null;

			return Lookups.GeneralLookup.Get(MethodNameAfter, v => t.GetMethod(MethodNameAfter));
		}

		public MethodInfo GetMethodBefore(Type t)
		{
			if (string.IsNullOrEmpty(MethodNameBefore))
				return null;

			return Lookups.GeneralLookup.Get(MethodNameBefore, v => t.GetMethod(MethodNameBefore));
		}
	}
}

